package com.alex.demo.controller;

import com.alex.demo.common.R;
import com.alex.demo.common.R;
import com.alex.demo.common.StatusCode;
import com.alex.demo.pojo.LieChe;
import com.alex.demo.service.LieCheService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Alex on 2022/3/26 10:58
 */
@Api(value = "获取同乘人员接口")
@RestController
@RequestMapping("/take")
public class TakeController {

    @Autowired
    LieCheService lieCheService;

    @ApiOperation(value = "根据证件编号获取Word", nickname = "根据证件编号获取Word", notes = "入参证件编号",produces = "application/octet-stream")
    @ApiImplicitParam(name = "idCard", value = "证件编号", required = true,defaultValue = "320683198110096010")
    @GetMapping("/getWord/{idCard}")
    public R getWord(HttpServletResponse response, @PathVariable String idCard){ //@RequestBody(required = false) LieChe lieCheQuery

        // TODO: 2022/3/28  接收从前台传输的用户信息
        //设置用户信息用以测试
        LieChe lieCheQuery = new LieChe();
        lieCheQuery.setIdCard(idCard);
        //获取主体所乘列车列表
        List<LieChe> list = lieCheService.getMsgList(lieCheQuery);

        //如果用户所乘列车并非一辆的筛选
        if ( null != list && list.size() > 0) {
            for (LieChe lieChe:list) {
                //生成密接人员word
                lieCheService.writeWord(response,lieChe);
                return null;
            }
        }else {
            return R.error().message("根据证件编号获取Word失败");
        }

        return null;
    }

    @ApiOperation(value = "根据证件编号获取Excel", notes = "入参为证件编号",produces = "application/octet-stream")
    @ApiImplicitParam(name = "idCard", value = "证件编号", required = true,defaultValue = "320683198110096010")
    @GetMapping("/getExcel/{idCard}")
    public R getExcel(@PathVariable String idCard, HttpServletResponse response){ //@RequestBody(required = false) LieChe lieCheQuery

        // TODO: 2022/3/28  接收从前台传输的用户信息
        //设置用户信息用以测试
        LieChe lieCheQuery = new LieChe();
        lieCheQuery.setIdCard(idCard);
        //获取主体所乘列车列表
        List<LieChe> list = lieCheService.getMsgList(lieCheQuery);

        //如果用户所乘列车并非一辆的筛选
        if ( null != list && list.size() > 0) {
            for (LieChe lieChe:list) {
                lieCheService.writerExcel(response,lieChe);
                return null;
            }
        }else {
            return R.ok().message("未查询到相关信息");
        }

        return null;

    }

    @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传Excel", notes = "上传Excel", httpMethod = "POST")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "上传成功！"),
            @ApiResponse(code = 500, message = "上传失败！")
    })
    public R upload(@ApiParam(value = "file", required = true) MultipartFile file) {
        if (file==null|| file.getSize()==0){
            System.out.println("1");
            return R.error().message("请上传文件");
        }
        String filename = file.getOriginalFilename();
        if (!(filename.endsWith(".xls")|| filename.endsWith(".xlsx"))){
            System.out.println("2");
        }

        lieCheService.uploadExcel(file);
        return R.ok().message("解析Excel成功");

    }

    @ApiOperation(value = "清空数据库", nickname = "清空数据库")
    @GetMapping("/clean")
    public R cleanDB(){

        Boolean b = lieCheService.clean();
        return R.ok().message("清空数据库成功");

    }

    @ApiOperation(value = "获取模板", nickname = "获取模板",produces = "application/octet-stream")
    @GetMapping("/getExample")
    public R getExample(HttpServletResponse response){
        lieCheService.getExample(response);
        return null;

    }

    @ApiOperation(value = "获取当前数据库信息", nickname = "获取当前数据库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true,defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "条数", required = true,defaultValue = "10")
    })
    @GetMapping("/getAll")
    public R getAll( long pageNum,long pageSize){
        //获取分页对象
        Page<LieChe> lieChePage = new Page<>(pageNum,pageSize);
        //构建条件
        LambdaQueryWrapper<LieChe> wrapper = new LambdaQueryWrapper<>();
        //获取数据库信息
        lieCheService.page(lieChePage, wrapper);
        //获取总记录数

        long total = lieChePage.getTotal();
        //获取数据集合
        List<LieChe>  records = lieChePage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

}
